package com.logindemo.filter;


import com.logindemo.domain.LoginAccount;
import com.logindemo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 认证token过滤逻辑：
     * 1.从请求头里面拿到token，看访问接口的用户是否携带token，未携带token使用filterChain直接放行
     * 2.toekn存在，进行token解析，从jwt的内容里面拿到用户id
     * 3.拿着用户id去redis查询用户信息并转换为LoginAccount对象，如果redis里面拿不到数据就代表用户登录超时
     * 4.拿到用户信息后，交由UsernamePasswordAuthenticationToken认证用户
     * 5.最后将认证的用户设置到springSecurity上下文，放行
     * @param request
     * @param response
     * @param filterChain
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {

    }
}
